package Sort;

import java.util.Arrays;

//希尔排序
public class ShellSort {
    public static void main(String[] args) {
        int[] arr = new int[]{1,3,5,2,4,6,0};
        shell2(arr);
        System.out.println(Arrays.toString(arr));
    }
    public static void shell(int[] arr){
        int  gap = arr.length;
        while(gap > 1){
            shellSort(arr,gap);
            gap/=2;
        }
        shellSort(arr,1);
    }
    public static void shellSort(int[] arr , int gap){
        for(int i = gap;i < arr.length;i++){
            int temp = arr[i];
            int j = i-gap;
            for (;j>=0;j-=gap){
                if(arr[j] > temp){
                    arr[j+gap] = arr[j];
                } else{
                    break;
                }
            }
            arr[j+gap] = temp;
        }
    }
    public static void shell2(int[] arr){
        int gap = arr.length;
        while(gap > 1){
            shellSort2(arr,gap);
            gap/=2;
        }
        shellSort2(arr,1);
    }
    public static void shellSort2(int[] arr,int gap){
        for (int i = gap;i < arr.length;i++){
            int temp = arr[i];
            int j = i-gap;
            for (;j>=0;j-=gap){
                if(arr[j] > temp){
                    arr[j+gap] = arr[j];
                }else {
                    break;
                }
            }
            arr[j+gap] = temp;
        }
    }
}
